﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Power.Model;
using System.Data;
using System.Data.SqlClient;

namespace Power.DAL
{
    public class MonitorDAL
    {


        public Monitorc ToFind(string device)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Monitor where Device=@device order by ID desc", new SqlParameter("@device", device));
            Monitorc mon = new Monitorc();

            mon = ToMon(dt.Rows[0]);

            return mon;
        }



        private Monitorc ToMon(DataRow row)
        {
            Monitorc op = new Monitorc();
            op.Id = (long)row["Id"];
            op.Device = (string)row["Device"];
            op.TempI = (string)row["TempI"];
            op.TempO = (string)row["TempO"];
            op.GPSL = (string)row["GPSL"];
            op.GPSW = (string)row["GPSW"];
            op.DTime = (string)row["DTime"].ToString();
            return op;
        }



        public Monitorc[] ToFindError()
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Monitor where Dtime>@DATEY and Dtime<@DATET and TempD<10", new SqlParameter("@DATEY", (DateTime.Now.AddDays(-1)).ToShortDateString()), new SqlParameter("@DATET", (DateTime.Now).ToShortDateString()));
            Monitorc[] mons = new Monitorc[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                mons[i] = ToMon(dt.Rows[i]);
            }



            return mons;
        }
















        public Monitorc[] ToFindYes(string device)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Monitor where Dtime>@DATEY and Dtime<@DATET and Device=@device", new SqlParameter("@device", device), new SqlParameter("@DATEY", (DateTime.Now.AddDays(-1)).ToShortDateString()), new SqlParameter("@DATET", (DateTime.Now).ToShortDateString()));
            Monitorc[] mons = new Monitorc[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                mons[i] = ToMon(dt.Rows[i]);
            }



            return mons;
        }



        public Monitorc[] ToFindYes2(string device)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Monitor where Dtime>@DATEY and Dtime<@DATET and Device=@device", new SqlParameter("@device", device), new SqlParameter("@DATEY", (DateTime.Now).ToShortDateString()), new SqlParameter("@DATET", (DateTime.Now.AddDays(1)).ToShortDateString()));
            Monitorc[] mons = new Monitorc[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                mons[i] = ToMon2(dt.Rows[i]);
            }



            return mons;
        }


        public Monitorc[] ToFindDay(string device,string d1s,string d2s)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Monitor where Dtime>@DATEY and Dtime<@DATET and Device=@device", new SqlParameter("@device", device), new SqlParameter("@DATEY", d1s), new SqlParameter("@DATET", d2s));
            Monitorc[] mons = new Monitorc[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                mons[i] = ToMon2(dt.Rows[i]);
            }



            return mons;
        }




        private Monitorc ToMon2(DataRow row)
        {
            Monitorc op = new Monitorc();
            op.Id = (long)row["Id"];
            
            op.TempI = (string)row["TempI"];
            op.TempO = (string)row["TempO"];
            op.GPSL = (string)row["GPSL"];
            op.GPSW = (string)row["GPSW"];
            op.DTime = row["DTime"].ToString();
            return op;
        }



        public Monitorc[] ToFindList(string device)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Monitor where Device=@Deivce and Dtime>@DATEY and Dtime<@DATET and TempD<10", new SqlParameter("@DATEY", (DateTime.Now.AddDays(-1)).ToShortDateString()), new SqlParameter("@DATET", (DateTime.Now).ToShortDateString()), new SqlParameter("@Deivce", device));
            Monitorc[] mons = new Monitorc[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                mons[i] = ToMon(dt.Rows[i]);
            }



            return mons;
        }


















    }
}
